﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using System.Data;
using System.Web.SessionState;
using WeChat.Common;
using System.IO;
using System.Text;
using System.Web.Script.Serialization;
using System.Configuration;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using RsaDemo;

namespace WeChat
{
    public partial class RetCallback : System.Web.UI.Page
    {
        string md5key = ""; //get from getKey return(key: md5)

        string publickey = "";
#pragma warning disable CS0414 // 字段“RetCallback.yzm”已被赋值，但从未使用过它的值
        string yzm = "";
#pragma warning restore CS0414 // 字段“RetCallback.yzm”已被赋值，但从未使用过它的值
        protected void Page_Load(object sender, EventArgs e)
        {
            ApplicationLog.Write("------------------回调页面入口：-----------------------");
            Secretkey();//获取密钥
            Stream stream = Request.InputStream;
            int len = (int)stream.Length;
            byte[] inputbytes = new byte[len];
            stream.Read(inputbytes, 0, len);
            stream.Close();
            string data = Encoding.GetEncoding("utf-8").GetString(inputbytes);
            JObject jo = (JObject)JsonConvert.DeserializeObject(data);
            ApplicationLog.Write("/////开始--回调页json字符串:" + data + "--结束//////////");
            string sign = jo["sign"].ToString();
            ApplicationLog.Write("sign:" + sign);
            string f = jo["f"].ToString();
            string dt = jo["d"]["dt"].ToString();
            string gj = jo["d"]["gj"].ToString();
            string go = jo["d"]["go"].ToString();
            string hj = jo["d"]["hj"].ToString();
            string j = jo["d"]["j"].ToString();
            string o1 = jo["d"]["o"].ToString();
            string oo = jo["d"]["oo"].ToString();
            string q1 = jo["d"]["q"].ToString();
            string qt = jo["d"]["qt"].ToString();
            string sj = jo["d"]["sj"].ToString();
            string t = jo["d"]["t"].ToString();
            string tj = jo["d"]["tj"].ToString();
            string u = jo["d"]["u"].ToString();
            ApplicationLog.Write("dt:" + dt);
            string signStr = "dt=" + dt + "&gj=" + gj + "&go=" + go + "&hj=" + hj + "&j=" + j + "&o=" + o1 + "&oo=" + oo + "&q=" + q1 + "&qt=" + qt + "&sj=" + sj + "&t=" + t + "&tj=" + tj + "&u=" + u + "&key=" + md5key;


            var signVerify = "";
            try
            {
                signVerify = MD5Sign.EncryptionMD5(signStr).ToUpper();
                ApplicationLog.Write("signVerify:" + signVerify);
                if (sign == signVerify)
                {

                    string opId = Request.QueryString["opId"];
                    string money = Request.QueryString["money"];
                    string q = Request.QueryString["q"];
                    string RtnMsg = "";
                    string business_no = Request.QueryString["business_no"];
                    string tn = Request.QueryString["tn"];
                    //通过opid查询卡号
                    string ryid = ReBLL.SelectRyid(opId);
                    //获取订单号
                    string o = ReBLL.OrderRyid(ryid);
                    string Types = Request.QueryString["Types"];
                    if (q == "1")
                    {
                        RtnMsg = "支付宝";
                    }
                    else if (q == "2")
                    {
                        RtnMsg = "微信";
                    }
                    else if (q == "3")
                    {
                        RtnMsg = "百度百付宝";
                    }
                    else if (q == "4")
                    {
                        RtnMsg = "苏宁易付宝";
                    }
                    else if (q == "6")
                    {
                        RtnMsg = "京东钱包";
                    }
                    else if (q == "7")
                    {
                        RtnMsg = "QQ钱包";
                    }
                    else if (q == "8")
                    {
                        RtnMsg = "翼支付";
                    }
                    ////查询订单是否成功
                    //string json = "{\"a\":\"payChk\",\"o\":\"" + o + "\",\"tn\":\"" + tn + "\"}";
                    //string returnTxt = RsaDemo.Common.HttpPost("s=" + RsaDemo.Common.rsaEncode(publickey, json, business_no));
                    //IDictionary<string, object> rlt = RsaDemo.Common.JsonToDictionary(returnTxt);
                    switch (Types)
                    {
                        case "Recharge":
                            ApplicationLog.Write("f:" + f);
                            if (f == "1" && ryid != "" && o != "")
                            {
                                //充值余额
                                //ReBLL.ExecuteLiuShui(rfkh, Convert.ToDouble(money));
                                //同时更新余额+记录到lssj
                                ReBLL.Flowrecord(opId, Convert.ToDouble(money), RtnMsg);
                                ReBLL.OrderNum(o, Convert.ToDouble(money), ryid, Convert.ToDateTime(dt), 2);
                                ApplicationLog.Write("Ryid:" + ryid + " 支付成功");
                                ApplicationLog.Write("------------------业务逻辑完成-----------------------");
                                Response.Write("success");
                            }
                            break;
                        default:
                            break;
                    }
                }



            }
            catch (Exception ex)
            {

                ApplicationLog.Write("异常:" + ex.Message);
            }


        }
        //获取密钥
        public void Secretkey()
        {
            string business_no = ConfigurationManager.AppSettings["business_no"];
            string sn = ConfigurationManager.AppSettings["sn"];
            //此处自行验证各值合法性
            string json = "{\"a\":\"getKey\",\"e\":\"" + business_no + "\",\"sn\":\"" + sn + "\"}";
            string returnTxt = RsaDemo.Common.HttpPost("json=" + json);
            IDictionary<string, object> rlt = RsaDemo.Common.JsonToDictionary(returnTxt);
            if (rlt["f"].ToString() == "1")
            {
                IDictionary<string, object> d = (IDictionary<string, object>)rlt["d"];
                //this.richTextBox1.Text = d["k"].ToString();
                publickey = d["k"].ToString();
                md5key = d["md5"].ToString();
                //this.textBox5.Text = "0001";
            }
            else
            {
                //MessageBox.Show(rlt["m"].ToString());
            }
        }
    }
}